package com.peoit.honeycomb.rest;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.peoit.honeycomb.application.CommentsApplication;
import com.peoit.honeycomb.domain.exception.ProjectNotExistException;
import com.peoit.honeycomb.domain.exception.UserNotExistException;
import com.peoit.honeycomb.dto.CommentsDto;
import com.peoit.honeycomb.dto.CommonResultDto;

@Controller
@RequestMapping("/api/comments")
public class CommentsResource extends BaseResource {
	Logger logger = Logger.getLogger(UserResource.class.getName());

	@Autowired
	CommentsApplication commentsApplication;

	@RequestMapping(value = "/publishcomments", method = RequestMethod.POST)
	public @ResponseBody CommonResultDto publishComments(String discussantId, String projectId, String content) {
		CommonResultDto result = new CommonResultDto();
		try {
			commentsApplication.publishComment(projectId, discussantId, content);
			result.setReason("success");
			result.setResult_code("200");
		} catch (UserNotExistException e) {
			result.setReason("User not exist");
			result.setResult_code("402");
		} catch (ProjectNotExistException e) {
			result.setReason("project not exist");
			result.setResult_code("401");
		} catch (Exception e) {
			result.setReason("Inter error");
			result.setResult_code("500");
		}
		return result;
	}

	/**
	 * 获取项目评论列表
	 * 
	 * @param projectId
	 * @return
	 */
	@RequestMapping(value = "/listcomments", method = RequestMethod.POST)
	public @ResponseBody CommonResultDto listComments(String projectId) {
		CommonResultDto result = new CommonResultDto();
		List<CommentsDto> dtos = commentsApplication.getProjectComments(projectId);
		result.setResult(dtos);
		result.setReason("success");
		result.setResult_code("200");
		return result;
	}
}
